<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
    * push 添加一个元素到栈顶
    * pop 出栈
    * peek 返回栈顶
    * isEmpty 判断是不是空
    * clear 清楚栈
    * size 返回栈的长度
    * toString 转为字符串
    * */
    class Stack{
        item = [];
        push(data){
            this.item.push(data)
        }
        pop(){
           return this.item.pop()
        }
        peek(){
            return this.item.at(-1)
        }
        isEmpty(){
            return this.item.length === 0
        }
        clear(){
            this.item = []
        }
        size(){
            return this.item.length
        }
        toString(){
            return this.item.join('-')
        }
    }
    let stack = new Stack()
    stack.push(1)
    stack.push(2)
    stack.push(3)
    console.log(stack.item) //[1,2,3]
    console.log(stack.peek()) // 3
    console.log(stack.toString()) // 1-2-3
    let out = stack.pop()
    console.log(out,stack.item) // 3 [1,2]
</script>
</body>
</html>
